package com.py.comment.interceptor;

import com.py.model.user.pojos.ApUser;
import com.py.utils.common.ApUserThreadLocalUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author pengy
 * @create 2022-11-17 10:48
 */
public class CommentInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("userId");
        if(!StringUtils.isEmpty(userId)){
            //存用户数据
            ApUser apUser = new ApUser();
            apUser.setId(Integer.parseInt(userId));
            ApUserThreadLocalUtil.set(apUser);
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ApUserThreadLocalUtil.clear();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
